package com.yanzhenjie.andserver.sample;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 作者: Tao
 * 日期: 2023/2/21
 * 时间: 10:08
 * 描述: session过期时间管理
 */
public class SessionExpireManager {
    private static final ConcurrentHashMap<String, Long> SESSION_EXPIRED_POOL = new ConcurrentHashMap<>();

    public static void refreshExpired(String key) {
        SESSION_EXPIRED_POOL.put(key, new Date().getTime() + 5 * 60 * 1000);
    }

    public static Long remove(String key) {
        return SESSION_EXPIRED_POOL.remove(key);
    }

    public static Long get(String key) {
        return SESSION_EXPIRED_POOL.get(key);
    }

    public static int size() {
        return SESSION_EXPIRED_POOL.size();
    }

    public static boolean containsKey(String key){
        return SESSION_EXPIRED_POOL.containsKey(key);
    }

    public static boolean isExpired(String key) {
        try {
            return new Date().getTime() - get(key) < 5 * 60 * 1000;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static ConcurrentHashMap<String, Long> getSessionExpiredPool() {
        return SESSION_EXPIRED_POOL;
    }
}
